package com.example.myui.activity.permission;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnDismissListener;
import com.bigkoo.alertview.OnItemClickListener;
import com.example.myui.R;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.logging.Logger;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

public class PermissionActivity extends SwipeBackActivity {

    private static final String TAG = PermissionActivity.class.getSimpleName();
    private AlertView mAlertView;
    private OnItemClickListener listener = new OnItemClickListener() {
        @Override
        public void onItemClick(Object o, int position) {
            Log.i(TAG, "onItemClick: " + position);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission);
        SwipeBackLayout swipeBackLayout = getSwipeBackLayout();
        swipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
//        saveTrackingMode(edgeFlag);
    }

    //打开相机权限 方式一
    public void doOpenCamera(View view) {
        RxPermissions rxPermissions = new RxPermissions(this);
//        rxPermissions
//                .requestEach(Manifest.permission.CAMERA,
//                        Manifest.permission.READ_PHONE_STATE)
//                .subscribe(permission -> { // will emit 2 Permission objects
//                    if (permission.granted) {
//                        // `permission.name` is granted !
//                        Toast.makeText(PermissionActivity.this, "granted", Toast.LENGTH_SHORT).show();
//                    } else if (permission.shouldShowRequestPermissionRationale) {
//                        // Denied permission without ask never again
//                        Toast.makeText(PermissionActivity.this, "shouldShowRequestPermissionRationale", Toast.LENGTH_SHORT).show();
//                    } else {
//                        // Denied permission with ask never again
//                        // Need to go to the settings
//                        Intent localIntent = new Intent();
//                        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                        if (Build.VERSION.SDK_INT >= 9) {
//                            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
//                            localIntent.setData(Uri.fromParts("package", getPackageName(), null));
//                        } else if (Build.VERSION.SDK_INT <= 8) {
//                            localIntent.setAction(Intent.ACTION_VIEW);
//                            localIntent.setClassName("com.android.settings","com.android.settings.InstalledAppDetails");
//                            localIntent.putExtra("com.android.settings.ApplicationPkgName", getPackageName());
//                        }
//                        startActivity(localIntent);
////                        Toast.makeText(PermissionActivity.this, "never ask again", Toast.LENGTH_SHORT).show();
//                    }
//                });

        rxPermissions.request(Manifest.permission.READ_PHONE_STATE, Manifest.permission.CAMERA)
                .subscribe(new Observer<Boolean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Boolean aBoolean) {

                        if (aBoolean) {
                            Log.i(TAG, "onNext: " + aBoolean);
                            Toast.makeText(PermissionActivity.this, "success", Toast.LENGTH_SHORT).show();
                        } else {
                            Log.i(TAG, "onNext: " + aBoolean);
                            Toast.makeText(PermissionActivity.this, "failed", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void doOpenCamera1(View view) {
        int camera = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
        int read_phone = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);
        if (camera != PackageManager.PERMISSION_GRANTED || read_phone != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_PHONE_STATE}, 1001);

        } else {
            Toast.makeText(this, "sucess", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1001) {

            boolean granted = false;
            for (int i = 0; i < grantResults.length; i++) {
                if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                    granted = true;
                }
            }

            if (granted) {
                Toast.makeText(this, getString(R.string.permission_deny), Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, getString(R.string.permission_granted), Toast.LENGTH_SHORT).show();
            }
        }
    }
}
